﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Wyse.Server.Base
{
    /// <summary>
    /// Server module - represents a service exposed by the server
    /// (e.g. HTTP server)
    /// </summary>
    public abstract class ServerModule
    {
        /// <summary>
        /// Ctor
        /// </summary>
        public ServerModule(String name)
        {
            Name = name;
        }

        /// <summary>
        /// Creates the listeners associated with this module
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Listener> CreateListeners()
        {
            if (mListenersCreated)
                throw new InvalidOperationException("Listeners already created");

            return CreateListenersImpl();
        }

        /// <summary>
        /// Initializes the module
        /// </summary>
        public abstract void Init();

        /// <summary>
        /// Gets the module's name
        /// </summary>
        public String Name
        {
            get;
            private set;
        }

        /// <summary>
        /// Implementation responsible for the actual creation
        /// of listeners - to be overriden in concrete module classes
        /// </summary>
        /// <returns>an IEnumerable holding all the listeners</returns>
        protected abstract IEnumerable<Listener> CreateListenersImpl();

        protected bool mListenersCreated;
    }
}
